package com.woniuxy.microgoodsserver.goodmag.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microcommonserver.bean.gy.*;
import com.woniuxy.microcommonserver.pageHelper.PageRequest;
import com.woniuxy.microcommonserver.pageHelper.TableResult;
import com.woniuxy.microcommonserver.res.ResultMsg;
import com.woniuxy.microgoodsserver.goodmag.service.GoodService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**商品管理
 * @author ：苟杨
 * @description：TODO
 * @date ：2021/1/26 9:47
 */
@RequestMapping("goods")
@RestController
public class GoodController {

    @Resource
    private GoodService goodServiceImpl;
    /**
        发布商品
     * @params: goodsInfo:商品的信息，goodsAttributesInfo：商品的sku信息
     * @return 
     */
    @PostMapping(value = "/addGoods",produces = MediaType.APPLICATION_JSON_VALUE)
    public String addGood(@RequestBody GoodsInfo goodsInfo){
        goodServiceImpl.addGood(goodsInfo);
        return JSONObject.toJSONString(ResultMsg.builder().code(200).msg("添加成功").build());
    }

    /**
        查询所有商品信息
     * @params:
     * @return
     */
    @RequestMapping(value = "/findAllGoods",produces = MediaType.APPLICATION_JSON_VALUE)
    public TableResult findAllGoods(PageRequest pageRequest, GoodsInfo goodsInfo){
        System.out.println(pageRequest);
        System.out.println(goodsInfo);
        PageInfo<List<Map>>  allGoods = goodServiceImpl.findAllGoods(pageRequest, goodsInfo);
        return new TableResult(allGoods.getTotal(),allGoods.getList());
    }

    /**
        根据ID删除商品信息
     * @params:
     * @return
     */
    @DeleteMapping(value = "/deleteGood",produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteGood(Integer id){
        goodServiceImpl.deleteGood(id);
        return JSONObject.toJSONString(ResultMsg.builder().code(200).msg("删除成功").build());
    }

    /**
        修改商品信息包括Sku属性
     * @params:
     * @return
     */
    @PutMapping(value = "/updateGood",produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateGood(@RequestBody GoodsInfo goodsInfo){
        System.out.println(goodsInfo);
        goodServiceImpl.updateGood(goodsInfo);
        return JSONObject.toJSONString(ResultMsg.builder().code(200).msg("修改成功").build());
    }

    /**
        根据ID查询商品名称
     * @params:
     * @return
     */
    @GetMapping(value = "/findGoodById",produces = MediaType.APPLICATION_JSON_VALUE)
    public String findGoodById(Integer id){
        GoodsInfo goodsInfo = goodServiceImpl.findGoodById(id);
        return JSONObject.toJSONString(goodsInfo);
    }
}
